using SmartCommunity.Services.Community;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.Controllers.Community
{
    /// <summary>
    /// API接口：
    /// </summary>
    [ApiController]
    [Route("api/community/[controller]")]
    public class RoomController : BaseController
    {
        #region 构造函数
        private readonly RoomService roomService;
        public RoomController(RoomService roomService)
        {
            this.roomService = roomService;
        }
        #endregion

        #region 查询导出 

        [HttpGet("building-tree")]
        public async Task<IEnumerable<SelectOption>> GetBuildingTree()
        {
            return await roomService.GetBuildingTree();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<RoomDto>> GetList([FromQuery] RoomQuery query, [FromQuery] Pager pager)
        {
            return await roomService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<RoomDto> GetOne(Guid id)
        {
            return await roomService.FindRoomById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] RoomQuery query)
        {
            var pageData = await roomService.FindPagedList(query, Pager.Unpaged, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "社区名称", "小区名称", "楼房编号", "所在楼层", "房间用途", "房间户型", "房间状态", "装修状态", "建筑面积", "使用面积", "备注说明");
            int rowIndex = 1;
            foreach (var data in pageData.DataList)
            {
                exporter.AddDataRow(rowIndex++, 0, data.CommunityName, data.AreaName, data.RoomCode, data.RoomFloor, data.RoomUsageName, data.RoomTypeName, data.RoomStateName, data.RoomDecoName, data.BuildArea, data.UsageArea, data.Remark);
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        [Log("新增", OperateType.Create, false, true)]
        public async Task<ActionResult<RoomDto>> Create([FromBody] RoomDto dto)
        {
            var room = await roomService.CreateRoom(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = room.RoomId }, room);
        }

        /// <summary>
        /// 修改
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改", OperateType.Update)]
        public async Task<ActionResult> Update(Guid id, [FromBody] RoomDto dto)
        {
            if (id != dto.RoomId)
            {
                return ErrorMessage("要更新的数据和ID不匹配");
            }
            await roomService.UpdateRoom(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpDelete]
        [Log("删除", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] Guid[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await roomService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
